package com.example.a13001.shoppingmalltemplate.presenter;

import android.content.Context;
import android.content.Intent;

import com.example.a13001.shoppingmalltemplate.manager.DataManager;
import com.example.a13001.shoppingmalltemplate.modle.Address;
import com.example.a13001.shoppingmalltemplate.modle.AnswerList;
import com.example.a13001.shoppingmalltemplate.modle.CommonResult;
import com.example.a13001.shoppingmalltemplate.mvpview.AdressManagerView;
import com.example.a13001.shoppingmalltemplate.mvpview.QuestionAndAnswerView;
import com.example.a13001.shoppingmalltemplate.mvpview.View;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class QuestionAndAnswerPredenter implements Presenter{
    private DataManager manager;
    private CompositeSubscription mCompositeSubscription;
    private Context mContext;
    private QuestionAndAnswerView mQuestionAndAnswerView;
    private AnswerList mAnswerList;
    private CommonResult mCommonResult;



    public QuestionAndAnswerPredenter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void onCreate() {
        mCompositeSubscription=new CompositeSubscription();
        manager=new DataManager(mContext);
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onStop() {
        if (mCompositeSubscription.hasSubscriptions()){
            mCompositeSubscription.unsubscribe();
        }
    }

    @Override
    public void pause() {

    }

    @Override
    public void attachView(View view) {
        mQuestionAndAnswerView=(QuestionAndAnswerView) view;
    }

    @Override
    public void attachIncomingIntent(Intent intent) {

    }

    /**
     * ★ 发布评论/咨询信息
     * @param companyid
     * @param code
     * @param timestamp
     * @param id                     评论内容ID
     * @param commentname            评论人名字，为空时会取会员昵称，如果未登录，则默认为游客
     * @param commentcontent           评论内容
     * @param commentimages              评论图片，多个用“,”号分割
     * @return
     */
    public void doCommentAdd(String companyid, String code, String timestamp,String id,String commentname,String commentcontent,String commentimages) {

        mCompositeSubscription.add(manager.doCommentAdd(companyid,code,timestamp,id,commentname,commentcontent,commentimages)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CommonResult>() {
                    @Override
                    public void onCompleted() {
                        if (mQuestionAndAnswerView!=null){
                            mQuestionAndAnswerView.onSuccessCommitComment(mCommonResult);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mQuestionAndAnswerView.onError("请求失败"+e.toString());
                    }

                    @Override
                    public void onNext(CommonResult commonResult) {
                        mCommonResult=commonResult;
                    }
                }));
    }
    /**
     * 获取评论/咨询列表信息
     * @param companyid      站点ID
     * @param id            评论内容ID
     * @param keyword       搜索关键词，模糊搜索评论人或评论内容
     * @param order         排序方式 0 按评论时间正序 1 按评论时间倒序 2 按支持数正序 3 按支持数倒序
     * @param pagesize      每页显示数量
     * @param pageindex      当前页数
     * @param from           来源，pc 电脑端 mobile 移动端 app app端 xcx 微信小程序
     * @return
     */
    public void getCommentList(String companyid, String id, String keyword, String order, int pagesize, int pageindex, String from) {

        mCompositeSubscription.add(manager.getCommentList(companyid,id,keyword,order,pagesize,pageindex,from)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<AnswerList>() {
                    @Override
                    public void onCompleted() {
                        if (mQuestionAndAnswerView!=null){
                            mQuestionAndAnswerView.onSuccessGetCommentList(mAnswerList);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mQuestionAndAnswerView.onError("请求失败"+e.toString());
                    }

                    @Override
                    public void onNext(AnswerList answerList) {
                        mAnswerList=answerList;
                    }
                }));
    }

}
